package guiTesting;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import com.atlassian.crowd.exception.ApplicationPermissionException;
import com.atlassian.crowd.exception.InvalidAuthenticationException;
import com.atlassian.crowd.exception.OperationFailedException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class SearchInputForm extends JInternalFrame {
	private 	RetrieveUserInfo userInfo;
	private 	List searchResults;
;
	/**
	 * Create the frame.
	 */
	public SearchInputForm() {
		setFrameIcon(new ImageIcon(SearchInputForm.class.getResource("/resources/bby.gif")));
		setTitle("Search");
		setResizable(true);
		setBounds(0, 0, 602, 337);
		
		JPanel searchInputPanel = new JPanel();
		searchInputPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Search", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 255, 0)));
		
		JPanel searchButtonPanel = new JPanel();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(searchButtonPanel, GroupLayout.PREFERRED_SIZE, 565, GroupLayout.PREFERRED_SIZE)
						.addComponent(searchInputPanel, GroupLayout.PREFERRED_SIZE, 529, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(11, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(searchInputPanel, GroupLayout.PREFERRED_SIZE, 221, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(searchButtonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(22, Short.MAX_VALUE))
		);
		
		JLabel lblUsernameSearch = new JLabel("Search Term");
		
		final JTextField textFieldSearch = new JTextField();
		textFieldSearch.setToolTipText("enter search term");
		textFieldSearch.setColumns(10);
		
		JPanel panelSearchTermNotes = new JPanel();
		panelSearchTermNotes.setBorder(new TitledBorder(null, "Notes", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout gl_panelSearchTermNotes = new GroupLayout(panelSearchTermNotes);
		gl_panelSearchTermNotes.setHorizontalGroup(
			gl_panelSearchTermNotes.createParallelGroup(Alignment.LEADING)
				.addGap(0, 157, Short.MAX_VALUE)
		);
		gl_panelSearchTermNotes.setVerticalGroup(
			gl_panelSearchTermNotes.createParallelGroup(Alignment.LEADING)
				.addGap(0, 152, Short.MAX_VALUE)
		);
		panelSearchTermNotes.setLayout(gl_panelSearchTermNotes);
		GroupLayout gl_searchInputPanel = new GroupLayout(searchInputPanel);
		gl_searchInputPanel.setHorizontalGroup(
			gl_searchInputPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_searchInputPanel.createSequentialGroup()
					.addGap(21)
					.addComponent(lblUsernameSearch, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
					.addGap(4)
					.addComponent(textFieldSearch, GroupLayout.PREFERRED_SIZE, 286, GroupLayout.PREFERRED_SIZE)
					.addGap(56)
					.addComponent(panelSearchTermNotes, GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_searchInputPanel.setVerticalGroup(
			gl_searchInputPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_searchInputPanel.createSequentialGroup()
					.addGap(25)
					.addGroup(gl_searchInputPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblUsernameSearch, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldSearch, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE))
					.addGap(134))
				.addGroup(gl_searchInputPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panelSearchTermNotes, GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
					.addContainerGap())
		);
		searchInputPanel.setLayout(gl_searchInputPanel);
		
		JButton searchButton = new JButton("Search");
		searchButton.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
			}
		});
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				userInfo = new RetrieveUserInfo();
				try {
					searchResults = userInfo.searchUserNames((textFieldSearch.getText()));
				} catch (OperationFailedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvalidAuthenticationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ApplicationPermissionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		GroupLayout gl_searchButtonPanel = new GroupLayout(searchButtonPanel);
		gl_searchButtonPanel.setHorizontalGroup(
			gl_searchButtonPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_searchButtonPanel.createSequentialGroup()
					.addGap(191)
					.addComponent(searchButton, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
					.addGap(64)
					.addComponent(btnCancel, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE))
		);
		gl_searchButtonPanel.setVerticalGroup(
			gl_searchButtonPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_searchButtonPanel.createSequentialGroup()
					.addGap(26)
					.addGroup(gl_searchButtonPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(searchButton, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnCancel, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)))
		);
		searchButtonPanel.setLayout(gl_searchButtonPanel);
		getContentPane().setLayout(groupLayout);

	}
	
	
	public void creatEvents(){
		
	}
}
